package com.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.common.PageBean;
import com.health.common.PageQueryDTO;
import com.health.mapper.SetmealMapper;
import com.health.pojo.Setmeal;
import com.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author zhangzhen
 * @version 1.0
 * @date 2025/1/20 21:09
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageBean findPage(PageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getCurrentPage(), pageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.findPage(pageQueryDTO);
        return new PageBean(page.getTotal(), page.getResult());

    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public void add(Setmeal setmeal, List<Integer> ids) {
        setmealMapper.insert(setmeal);
        Integer id = setmeal.getId();
        setCurrentdd(id, ids);
    }

    public void setCurrentdd(Integer id, List<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            for (Integer i : ids) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("setmeal_id", id);
                map.put("checkgroup_id", i);
                setmealMapper.insertAnd(map);
            }
        }

    }

}